package com.googlecode.agscrum.controller.jsf.util;

import java.util.Iterator;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.googlecode.agscrum.model.exceptions.GScrumDomainException;
import com.googlecode.agscrum.model.exceptions.GScrumMessageException;

public class FacesUtil {
	public static void addMessage(String messageText, Severity typeMessage) {
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage message = new FacesMessage(typeMessage, messageText, null);
		context.addMessage(null, message);
	}

	public static UIComponent findComponentInRoot(String id) {

		UIComponent component = null;

		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (facesContext != null) {
			UIComponent root = facesContext.getViewRoot();
			component = findComponent(root, id);
		}

		return component;
	}

	@SuppressWarnings("unchecked")
	public static UIComponent findComponent(UIComponent base, String id) {
		if (id.equals(base.getId()))
			return base;

		UIComponent kid = null;
		UIComponent result = null;
		Iterator kids = base.getFacetsAndChildren();
		while (kids.hasNext() && (result == null)) {
			kid = (UIComponent) kids.next();
			if (id.equals(kid.getId())) {
				result = kid;
				break;
			}
			result = findComponent(kid, id);
			if (result != null) {
				break;
			}
		}
		return result;
	}

	public static String getRequestParam(String param) {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> resquestParams = context.getExternalContext()
				.getRequestParameterMap();
		return resquestParams.get(param);
	}

	public static HttpSession getSession() {
		FacesContext context = FacesContext.getCurrentInstance();
		return ((HttpServletRequest) context.getExternalContext().getRequest())
				.getSession();
	}

	public static ServletContext getServletContext() {
		return (ServletContext) FacesContext.getCurrentInstance()
				.getExternalContext().getContext();
	}

	public static void cleanSubmittedValues(UIComponent component) {
		if (component != null && component.getChildren() != null)
			component.getChildren().clear();
	}

	public static void addMessage(GScrumDomainException e) {
		for (GScrumMessageException gScrumMessageException : e.getMessages()) {
			Severity severity;
			switch (gScrumMessageException.getTypeMessage()) {

			case FATAL:
				severity = FacesMessage.SEVERITY_FATAL;
				break;
			case INFO:
				severity = FacesMessage.SEVERITY_INFO;
				break;
			case WARNING:
				severity = FacesMessage.SEVERITY_WARN;
				break;
			default:
				severity = FacesMessage.SEVERITY_ERROR;
			}
			FacesUtil.addMessage(gScrumMessageException.getMessage(), severity);
		}
	}
}
